﻿using BaseUtils.Logger;
using NW_WEB_Service.com.app.services;
using Service.src.Flow.JavaScript;
using Service.src.NW.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Service.src.Flow.Server
{
    public class GlobalParamService : DBService
    {
        public GlobalParamEntity AddGlobalParam(GlobalParamEntity GlobalParamInfo)
        {
            GlobalParamInfo = DBInstance.Install(GlobalParamInfo);
            return GlobalParamInfo;
        }

        /// <summary>
        /// 更新设备
        /// </summary>
        /// <param name="GlobalParam"></param>
        /// <returns></returns>
        public static int UpldateGlobalParam(GlobalParamEntity GlobalParam)
        {

            int i = DBInstance.Update(GlobalParam);
          
            return i;
        }

        public bool UpdateGlobalParam(GlobalParamEntity Update)
        {
            if (Update == null)
            {
                return false;
            }
            foreach (GlobalParamEntity Target in AppData.GlobalParams)
            {
                if (Update.Name == Target.Name)
                {
                    Target.Value = Update.Value;
                    JavaScriptEvent.Send(JavaScriptEventEnum.GLOBAL_PARAM_DATA_UPDATE, Target);
                    //Task.Run(() =>
                    //{
                    UpldateGlobalParam(Target);
                    //});
                    break;
                }
            }
            return true;
        }

        private static object lockObject = new object();

        public static GlobalParamEntity UpdateGlobalParam(int Id, string Value)
        {
            GlobalParamEntity g = null;
            foreach (GlobalParamEntity target in AppData.GlobalParams)
            {
                if (Id == target.Id)
                {
                    target.Value = Value;
                    try
                    {
                        lock (lockObject)
                        {
                            UpldateGlobalParam(target);
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggerService.Error("ajgr:" + ex.Message);
                    }
                    g = target;
                    break;
                }
            }
            if (g == null)
            {
                throw new Exception("未找到对应的全局变量，请检查后重试");
            }
            return g;
        }

        public List<GlobalParamEntity> GetGlobalParamlist()
        {
            List<GlobalParamEntity> list = DBInstance.QueryAll<GlobalParamEntity>();
            return list;
        }

        internal GlobalParamEntity GetGlobalParamInfoById(int Id)
        {
            GlobalParamEntity Info = DBInstance.Query<GlobalParamEntity>(i => i.Id == Id).FirstOrDefault();
            if (Info == null)
            {
                throw new Exception("获取设备异常，找不此设备");
            }
            return Info;
        }

        internal bool DeleteGlobalParam(int id)
        {
            return DBInstance.Delete(new GlobalParamEntity() { Id = id });
        }
    }
}
